package com.ttl.bc.stock.request2.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;


import com.ttl.bc.ic.finishgoods.db.BCFinishGoodsBO;
import com.ttl.bc.ic.stkissue.db.BCStkIssueBO;

import com.ttl.bc.stock.request2.db.BCStkIssue2BO;
import com.ttl.common.util.CommonUtils;

public class BCStkIssue2Action extends DispatchAction {

	public ActionForward blank(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
				
		String user 	 = (String)request.getSession().getAttribute("userName");		
		String issueType = (String)request.getParameter("issueType");
		String preDocNo  = (String)request.getSession().getAttribute("preDocNo");				
		
		String issueTypeName = CommonUtils.getIssueType(Integer.valueOf(issueType));				
		String docNo 		 = CommonUtils.getMaxDocNo(preDocNo);
		
//		request.getSession().setAttribute("userName", user);
		request.getSession().setAttribute("issueType", issueType);
		request.getSession().setAttribute("issueTypeName", issueTypeName);
		request.setAttribute("docNo", docNo);		
		request.setAttribute("docDate", CommonUtils.getDateForStamp());
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssue2Action #mode :blank");
		
		return mapping.findForward("blank");
	}	
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssue2Action #mode :add");
		
		BCStkIssue2BO bo = new BCStkIssue2BO();		
		
		if (bo.add(form, request, response)==true) {						
			return mapping.findForward("success_add");
		}else {
			//ทำ map กับการ fail_add
			return mapping.findForward("blank");
		}
	}
	
	public ActionForward updateForApprove(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {

		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssue2Action #mode :update");		
		
		BCStkIssue2BO bo = new BCStkIssue2BO();
		@SuppressWarnings("unused")
		boolean addComplete = bo.updateForApprove(form, request, response);
		

		//System.out.println("updateComplete -> "+addComplete);
						
		return mapping.findForward("success_updateForApprove");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {

		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssue2Action #mode :update");		
		
		BCStkIssue2BO bo = new BCStkIssue2BO();
		@SuppressWarnings("unused")
		boolean addComplete = bo.update(form, request, response);
		

		//System.out.println("updateComplete -> "+addComplete);
						
		return mapping.findForward("success_update");
	}	

	public ActionForward updatePrice(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {

		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssue2Action #mode :updatePrice");		
		
		BCStkIssue2BO bo = new BCStkIssue2BO();
		if (bo.updatePrice(form, request, response)==true) {
			return mapping.findForward("success_updatePrice");
		}else {
			return mapping.findForward("fail_updatePrice");
		}						
	}	
	public ActionForward beforeApprove(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssue2Action #mode :beforeApprove ");
			
		BCStkIssue2BO bo = new BCStkIssue2BO();
		BCStkIssue2Form issue2form = (BCStkIssue2Form)bo.findForm(request);
		
		request.setAttribute("issue2form", issue2form);
						
		return mapping.findForward("blank_beforeApprove");
	}	
	
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssue2Action #mode :edit ");
			
		BCStkIssue2BO bo = new BCStkIssue2BO();
		BCStkIssue2Form issue2form = (BCStkIssue2Form)bo.findForm(request);
		
		request.setAttribute("issue2form", issue2form);
						
		return mapping.findForward("blank_edit");
	}
	
	public ActionForward price(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssue2Action #mode :price ");
			
		BCStkIssue2BO bo = new BCStkIssue2BO();
		BCStkIssue2Form issue2form = (BCStkIssue2Form)bo.findForm(request);
		
		request.setAttribute("issue2form", issue2form);
						
		return mapping.findForward("blank_price");
	}	

	public ActionForward receiveConfirmShow(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
//		String user 	 = (String)request.getSession().getAttribute("userName");
//		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssue2Action #mode :receiveConfirmShow ");
//			
//		BCStkIssue2BO bo = new BCStkIssue2BO();
//		BCStkIssue2Form issue2form = (BCStkIssue2Form)bo.findForm(request);
//		
//		request.setAttribute("issue2form", issue2form);
//						
//		return mapping.findForward("blank_receiveConfirmShow");
//		BCReqConfirmBO bo = new BCReqConfirmBO();
//		BCStkRequestForm issueform = bo.approveShow(request, response);		
		BCStkIssueBO bo = new BCStkIssueBO();
		BCStkIssue2Form issueform = bo.approveShow(request, response);
		
		String inPage = (String)request.getParameter("d-49653-p");
		String user   = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssue2Action #mode :receiveConfirmShow "+
									  "#docNo:"+issueform.getDocNo());		
		
		String preDocNoFG = "TI-TT-";
		
		String fgDocNo = CommonUtils.getMaxFinishGoodsDocNo(preDocNoFG);
		
		request.setAttribute("issue2form", issueform);
		request.setAttribute("inPage", inPage);
		request.setAttribute("fgDocNo", fgDocNo);
		
		return mapping.findForward("blank_receiveConfirmShow");		
	}	
	
	@SuppressWarnings("unchecked")
	public ActionForward show(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String preDocNo  = (String)request.getSession().getAttribute("preDocNo");		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssue2Action #mode :show");
		
		BCStkIssue2BO bo = new BCStkIssue2BO();			
		ArrayList results = (ArrayList)bo.find(request);
		
		request.getSession().setAttribute("stkIssue2Data", results);	
		request.getSession().setAttribute("preDocNo", preDocNo);
		
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("MM");		
		String monthFind = sdf.format(date);
		
		request.setAttribute("monthFind", monthFind);
		
		return mapping.findForward("success_show");
	}		
	
	public ActionForward showFind(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssue2Action #mode :showFind");
		
		BCStkIssue2BO bo = new BCStkIssue2BO();					
		bo.showFind(request);
				
		return mapping.findForward("success_showFind2");
	}
	
	@SuppressWarnings("unchecked")
	public ActionForward showFindOLd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssue2Action #mode :showFind");
		
		BCStkIssue2BO bo = new BCStkIssue2BO();							
		ArrayList results = (ArrayList)bo.find(request);
		
		request.getSession().setAttribute("stkIssue2Data", results);
		
//		String monthFind = (String)request.getParameter("monthFind");
//		if (monthFind==null) {
//			monthFind = "0";
//		}
//		
//		request.setAttribute("monthFind", Integer.parseInt(monthFind));
		
		return mapping.findForward("success_showFind");
	}

	public ActionForward cancel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String inPage = (String)request.getParameter("d-49653-p");
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssue2Action #mode :cancel ");
			
		BCStkIssue2BO bo = new BCStkIssue2BO();
		BCStkIssue2Form issue2form = (BCStkIssue2Form)bo.findForm(request);
		
		request.setAttribute("issue2form", issue2form);
		request.setAttribute("inPage", inPage);		
		request.getSession().setAttribute("userName", user);
		
		return mapping.findForward("success_cancel");		
		
	}	
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssue2Action #mode :delete ");		
						
		BCStkIssue2BO bo = new BCStkIssue2BO();
														
		if (bo.delete(form, request, response)==true) {
			request.setAttribute("message", "ลบเอกสารเรียบร้อยแล้ว");
			return mapping.findForward("success_message");
		}else {
			//ทำ map กับการ fail_add
			return mapping.findForward("fail_delete");
		}						
	}
	
	public ActionForward cancelOfDelete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		String inPage = (String)request.getParameter("d-49653-p");
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssue2Action #mode :cancelOfDelete ");
			
		BCStkIssue2BO bo = new BCStkIssue2BO();
		BCStkIssue2Form issue2form = (BCStkIssue2Form)bo.findForm(request);
		
		request.setAttribute("issue2form", issue2form);
		request.setAttribute("inPage", inPage);		
		request.getSession().setAttribute("userName", user);		
		
		return mapping.findForward("success_cancelOfDelete");		
	}
	
	public ActionForward receiveCancelShow(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		String inPage = (String)request.getParameter("d-49653-p");
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssue2Action #mode :cancelOfDelete ");
			
		BCStkIssue2BO bo = new BCStkIssue2BO();
		BCStkIssue2Form issue2form = (BCStkIssue2Form)bo.findForm(request);
		
		new BCFinishGoodsBO().find(request, issue2form.getDocNo());
		
		
		
		request.setAttribute("issue2form", issue2form);
		request.setAttribute("inPage", inPage);						
		
		return mapping.findForward("receiveCancelShow");		
	}	
	
	public ActionForward restore(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssue2Action #mode :restore ");		
						
		BCStkIssue2BO bo = new BCStkIssue2BO();
														
		if (bo.restore(form, request, response)==true) {
			request.setAttribute("message", "เรียกเอกสารคืน เรียบร้อยแล้ว");
			return mapping.findForward("success_message");
		}else {
			//ทำ map กับการ fail_add
			return mapping.findForward("fail_restore");
		}						
	}	

	public ActionForward addToFinishGoods_TTL2(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
										
		String inPage = (String)request.getParameter("d-49653-p");
		String user   = (String)request.getSession().getAttribute("userName");
		
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssue2Action #mode :addToFinishGoods_TTL2 "+
									  "#docNo:"+request.getParameter("docNo"));		
										
		request.setAttribute("inPage", inPage);		
		
		BCFinishGoodsBO bo2 = new BCFinishGoodsBO();
		if (bo2.addToFinishGoods_TTL2_stkReq(request, response)==true) {
			return mapping.findForward("success_addToFinishGoods_TTL2");
		}else{
			return mapping.findForward("fail_addToFinishGoods_TTL2");
		}							
	}
	
	
	public ActionForward cancelToFinishGoods_TTL2(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
												
		String inPage = (String)request.getParameter("d-49653-p");
		String user   = (String)request.getSession().getAttribute("userName");
		
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssue2Action #mode :cancelToFinishGoods_TTL2 "+
									  "#docNo:"+request.getParameter("docNo"));		
										
		request.setAttribute("inPage", inPage);		
		
		BCFinishGoodsBO bo2 = new BCFinishGoodsBO();
		if (bo2.cancelToFinishGoods_TTL2_stkReq(request, response)==true) {
			return mapping.findForward("success_cancelToFinishGoods_TTL2");
		}else{
			return mapping.findForward("fail_cancelToFinishGoods_TTL2");
		}							
	}	
}
